﻿/*
(C)小草莓软件	https://www.xcmsoft.com/
QQ：406675425
Email：406675425@qq.com
*/
#include "stdafx.h"
#include "Hsvcs.h"
#include "HmjListBox.h"


// CHmjListBox

IMPLEMENT_DYNAMIC(CHmjListBox, CListBox)

CHmjListBox::CHmjListBox()
{
	SetColors(RGB(0, 0, 128), RGB(255, 255, 255), RGB(46, 55, 172), RGB(255, 255, 255), RGB(220, 220, 220));
	SetFontNameAndSize(L"楷体", 20);
	SetItemHeight(16);
}

CHmjListBox::~CHmjListBox()
{
	m_bkBrush.DeleteObject();
}

// 设置行高
// nItemHeight：输入参数，行高
void CHmjListBox::SetItemHeight(int nItemHeight)
{
	m_nItemHeight = nItemHeight;
}

// 设置颜色
// backColor：背景颜色
// textColor：文本颜色
// 选中项的背景颜色
// 选中项的文本颜色
void CHmjListBox::SetColors(COLORREF backColor, COLORREF textColor, COLORREF selectedBackColor, COLORREF selectedTextColor, COLORREF borderColor)
{
	m_backColor = backColor;
	m_textColor = textColor;
	m_selectedBackColor = selectedBackColor;
	m_selectedTextColor = selectedTextColor;
	m_borderColor = borderColor;

	m_bkBrush.DeleteObject();
	m_bkBrush.CreateSolidBrush(m_backColor);

	if (GetSafeHwnd())
		Invalidate();
}

// 设置字体名称和大小
// szFontName：输入参数，字体名称
// nFontSize：输入参数，字体大小
void CHmjListBox::SetFontNameAndSize(const CString& szFontName, long nFontSize)
{
	LOGFONT logfont = { 0 };
	memset(&logfont, 0, sizeof(LOGFONT));
	logfont.lfHeight = nFontSize;
	swprintf(logfont.lfFaceName, LF_FACESIZE, szFontName);

	m_font.DeleteObject();
	m_font.CreateFontIndirect(&logfont);

	if (GetSafeHwnd())
		Invalidate();
}


BEGIN_MESSAGE_MAP(CHmjListBox, CListBox)
	ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()



// CHmjListBox 消息处理程序


void CHmjListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	if (lpDrawItemStruct->CtlType != ODT_LISTBOX)
		return;

	CRect rcItem(lpDrawItemStruct->rcItem);
		
	LPCWSTR lpText = (LPCWSTR)lpDrawItemStruct->itemData;
	if (lpText == NULL)
		return;

	CString szText(lpText);
	
	CDC dc;
	dc.Attach(lpDrawItemStruct->hDC);

	COLORREF crOldTextColor = dc.GetTextColor();
	COLORREF crOldBkColor = dc.GetBkColor();
	CFont* pFont = dc.SelectObject(&m_font);

	BOOL bSelected = FALSE;
	if ((lpDrawItemStruct->itemAction | ODA_SELECT) && (lpDrawItemStruct->itemState & ODS_SELECTED))
	{
		bSelected = TRUE;
		dc.SetTextColor(m_selectedTextColor);
		dc.SetBkColor(m_selectedBackColor);
		dc.FillSolidRect(&rcItem, m_selectedBackColor);
	}
	else
	{
		dc.SetTextColor(m_textColor);
		dc.SetBkColor(m_backColor);
		dc.FillSolidRect(&rcItem, m_backColor);
	}

	rcItem.left += 2;
	CString szFlag = L"▶";
	CRect rcFlag(0, 0, 1, 1);
	dc.DrawText(szFlag, &rcFlag, DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_CALCRECT);

	if (bSelected)
	{		
		dc.DrawText(szFlag, &rcItem, DT_LEFT | DT_SINGLELINE | DT_VCENTER);
	}	

	rcItem.left += rcFlag.Width() + 4;
		
	dc.DrawText(szText, &rcItem, DT_LEFT | DT_SINGLELINE | DT_VCENTER);

	dc.SetTextColor(crOldTextColor);
	dc.SetBkColor(crOldBkColor);
	dc.SelectObject(pFont);

	dc.Detach();
}

void CHmjListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
	if (lpMeasureItemStruct->CtlType != ODT_LISTBOX)
		return;

	CClientDC dc(this);
	CFont* pFont = dc.SelectObject(&m_font);
	TEXTMETRIC tm = { 0 };
	dc.GetOutputTextMetrics(&tm);
	dc.SelectObject(pFont);

	lpMeasureItemStruct->itemHeight = max(m_nItemHeight, tm.tmHeight + 4);
}

HBRUSH CHmjListBox::CtlColor(CDC* /*pDC*/, UINT /*nCtlColor*/)
{
	return (HBRUSH)m_bkBrush;
}
